<?php
/**
 * 图片水印操作类.
 * User: xianfei.li@stosz.com
 * Version : 1.0
 * Date: 2017/8/1
 * Time: 13:46
 */
namespace admin\helper;
class ImageHander {

    protected $font = "./ttf/msyhbd.ttf";         //字体

    protected $imagesource = null;          //图片资源
    protected $imagesize = 0;               //图片大小
    protected $imagewidth = 0;              //宽度
    protected $imageheight = 0;             //高度
    protected $imageinfo = null;            //图片信息
    protected $imagemime;                   //图片MIME

    protected $mark_type = 'text';          //水印类型 text:文本 image:图片
    protected $mark_alpha = 70;             //水印透明度
    protected $mark_point = 0.25;           //水印基数点
    protected $mark_words = "";             //水印信息
    protected $mark_offset = 0;            //水印偏移量
    protected $mark_fontsize = 18;          //字体大小
    protected $mark_angle = 30;             //偏移角度

    public $savepath = "./";                //图片保存路径
    public $savename = "";                  //保存名称

    public function __construct($savepath,$mark = "",$type = "text",$alpha = 70,$angle = 30,$point = 0.25,$fontsize = 18)
    {
        $this->mark_alpha = $alpha;
        $this->mark_point = $point;
        $this->mark_type = $type;
        $this->mark_words = $mark;
        $this->mark_fontsize = $fontsize;
        $this->savepath = $savepath;
        $this->savename = Date("YmdHis",time()).rand(10000,99999);
    }

    public function createImageMark($image){
        $this->imageinfo = getimagesize($image);
        if(!$this->imageinfo){
            return "图片格式不正确，请提交正确的图片！";
        }
        $this->imagewidth = $this->imageinfo[0];  //图片宽度
        $this->imageheight = $this->imageinfo[1]; //图片高度
        $this->imagemime = $this->imageinfo["mime"];

        switch ($this->imagemime){
            case "image/jpeg":
            case "image/jpg":
                $this->imagesource = @imagecreatefromjpeg($image);
                break;
            case "image/gif":
                $this->imagesource = @imagecreatefromgif($image);
                break;
            case "image/png":
                $this->imagesource = @imagecreatefrompng($image);
                break;
        }

        if($this->mark_type == "text"){         //文本水印
            $this->mark_words = implode(" ",[$this->mark_words,$this->mark_words,$this->mark_words]);
            return $this->makeTextMark();
        }elseif($this->mark_type == "image"){   //图片水印
            return $this->makeImageMark();
        }

    }

    private function makeTextMark(){
        $white = @imagecolorallocatealpha($this->imagesource,255,255,255,$this->mark_alpha);
        $im_x = $this->imagewidth * $this->mark_point;
        $im_y = $this->imageheight + $this->mark_offset;
        ImageTTFText($this->imagesource,$this->mark_fontsize,$this->mark_angle,$im_x,$im_y,$white,$this->font,$this->mark_words);
        return $this->output();
    }

    private function makeImageMark(){
            //2.0版本待开发
            return null;
    }

    protected function output(){
        if($this->imagesource){

            //如果目录不存在，就创建
            if(!is_dir($this->savepath)){
                mkdir($this->savepath,0755);
            }

            $outpath = null;

            switch ($this->imagemime){
                case "image/jpeg":
                case "image/jpg":
                    $outpath = $this->savepath.$this->savename.".jpg";
                    imagejpeg($this->imagesource,$outpath);
                    break;
                case "image/png":
                    $outpath = $this->savepath.$this->savename.".png";
                    imagepng($this->imagesource,$outpath);
                    break;
                case "image/gif":
                    $outpath = $this->savepath.$this->savename.".gif";
                    imagegif($this->imagesource,$outpath);
                    break;
            }
            return $outpath;
        }else{
            return false;
        }
    }

    public function setFont($fontpath){
        if($fontpath) {
            $this->font = $fontpath;
        }
    }

    public function setMarkType($type){
        $this->mark_type = $type;
    }

    public function setMarkAlpha($alpha){
        $this->mark_alpha = $alpha;
    }

    public function setMarkPoint($point){
        $this->mark_point = $point;
    }

    public function setMarkWords($words){
        $this->mark_words = $words;
    }
}